package com.zt.rongda.stock.controller;

import com.zt.rongda.stock.model.StockTransferOut;
import com.zt.rongda.stock.service.StockTransferOutService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/stockTransferOut")
public class StockTransferOutController {

    @Autowired
    private StockTransferOutService stockTransferOutService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockTransferOutService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockTransferOutService.add(getStockTransferOut(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockTransferOutService.update(getStockTransferOut(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockTransferOutService.delete(getStockTransferOut(reqBean));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private StockTransferOut getStockTransferOut(ReqBean reqBean) {
        StockTransferOut stockTransferOut = new StockTransferOut();
        stockTransferOut.setListId(reqBean.getInfo("listId"));
        stockTransferOut.setStockId(reqBean.getInfo("stockId"));
        stockTransferOut.setStockName(reqBean.getInfo("stockName"));
        stockTransferOut.setTypeId(reqBean.getInfo("typeId"));
        stockTransferOut.setTypeName(reqBean.getInfo("typeName"));
        stockTransferOut.setSpec(reqBean.getInfo("spec"));
        stockTransferOut.setBrand(reqBean.getInfo("brand"));
        stockTransferOut.setUnit(reqBean.getInfo("unit"));
        stockTransferOut.setNum(reqBean.getInfoInt("num"));
        stockTransferOut.setPrice(reqBean.getInfo("price"));
        stockTransferOut.setTotalPrice(reqBean.getInfo("totalPrice"));
        stockTransferOut.setStockDate(reqBean.getInfoDate("stockDate"));
        stockTransferOut.setLocation(reqBean.getInfo("location"));
        stockTransferOut.setSupplierId(reqBean.getInfo("supplierId"));
        stockTransferOut.setSupplierName(reqBean.getInfo("supplierName"));
        stockTransferOut.setSettlement(reqBean.getInfo("settlement"));
        stockTransferOut.setPurpose(reqBean.getInfo("purpose"));
        stockTransferOut.setContent(reqBean.getInfo("content"));
        stockTransferOut.setUserId(reqBean.getInfo("userId"));
        stockTransferOut.setUserName(reqBean.getInfo("userName"));
        stockTransferOut.setType(reqBean.getInfo("type"));
        stockTransferOut.setOperator(reqBean.getUserId());
        stockTransferOut.setRemark(reqBean.getInfo("remark"));
        return stockTransferOut;
    }

}